package ham.es;

import com.alibaba.fastjson.JSONObject;
import ham.es.entity.Product;
import ham.es.utils.IdWorker;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentType;


public class CreateIndex {

	public void createIndex() throws Exception{

		TransportClient client = EsClient.getClient();

		for(int i=0;i<100;i++){
			Product product = new Product();
			Long id = new IdWorker().nextId();
			product.setId(id);
			product.setName("工薪贷"+i);
			product.setRemark("备注"+i);
			product.setCondition("条件"+i);
			JSONObject jsonObject = new JSONObject();
			String json = jsonObject.toJSONString(product);
			IndexResponse response = client.prepareIndex("products", "product",String.valueOf(id)).setSource(json, XContentType.JSON).get();
			System.out.println(i);
		}
		client.close();
	}
	
	
	public static void main(String[] args) {
		CreateIndex createIndex = new CreateIndex();
		try {
			createIndex.createIndex();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


